﻿'USAGE

'Timed
'gen(chamber(1), chamber(2), chamber(0), 10, 100, 1000)

'Background
'gen(chamber(1), chamber(0), chamber(3), -1, 0)
'off()

Public Class Control
    Private Shared portused As String
    Private Shared cmd() As Byte = {&H1, &H0, &H0, &HB, &H10, &H0, &H0, &H0, &H0, &H0, &H0}
    Private Shared countdown As Timers.Timer
    Private Shared buffer1() As Byte
    Private Shared buffer2() As Byte
    Private Shared buffer3() As Byte
    Private Shared bgsmell() As Byte = {&H0, &H0, &H0}
    Private Shared temp1 As Integer
    Private Shared temp2 As Integer
    Private Shared temp3 As Integer
    Public Shared Function chamber(ByVal int As Integer) As Byte()
        Dim cn As Byte
        Dim offset As Integer
        Dim power As Integer
        Dim chamnum() As Byte = {&H0, &H0, &H0}

        offset = Math.DivRem(int, 8, power)
        cn = Math.Pow(2, power)
        chamnum(2 - offset) = cn

        Return chamnum
    End Function
    Private Shared Sub cdconfig(ByVal time As Integer)
        countdown = New Timers.Timer(time)
        countdown.AutoReset = False
        AddHandler countdown.Elapsed, AddressOf degen
        countdown.Enabled = True
    End Sub
    Private Shared Sub send()
        Using com1 As IO.Ports.SerialPort = My.Computer.Ports.OpenSerialPort(portused, 19200, IO.Ports.Parity.None, 8, IO.Ports.StopBits.One)
            com1.DtrEnable = True
            com1.Write(cmd, 0, cmd.Length)
        End Using
    End Sub
    Public Shared Sub off()
        bgsmell(0) = &H0
        bgsmell(1) = &H0
        bgsmell(2) = &H0
        degen()
    End Sub
    Private Shared Sub degen()
        If temp1 = Nothing Or temp1 = 0 Then
            cmd(6) = bgsmell(0)
            cmd(7) = bgsmell(1)
            cmd(8) = bgsmell(2)
            send()
        ElseIf temp2 = Nothing Then
            gen(buffer1, temp1)
            temp1 = Nothing
        ElseIf temp3 = Nothing Then
            gen(buffer1, buffer2, temp1, temp2)
            temp2 = Nothing
        Else
            gen(buffer1, buffer2, buffer3, temp1, temp2, temp3)
            temp3 = Nothing
        End If
    End Sub
    Public Shared Sub gen(ByVal data1() As Byte, Optional ByVal t1 As Integer = 0)
        If portused = Nothing Then
            If check() = True Then
                gen(data1, t1)
            End If
        Else
            cmd(6) = &H10 + data1(0)
            cmd(7) = data1(1)
            cmd(8) = data1(2)
            If t1 > 0 Then
                cdconfig(t1)
            Else
                bgsmell(0) = cmd(6)
                bgsmell(1) = cmd(7)
                bgsmell(2) = cmd(8)
            End If
            send()
        End If
    End Sub
    Public Shared Sub gen(ByVal data1() As Byte, ByVal data2() As Byte, Optional ByVal t1 As Integer = 0, Optional ByVal t2 As Integer = 0)
        If portused = Nothing Then
            If check() = True Then
                gen(data1, data2, t1, t2)
            End If
        Else
            cmd(6) = &H10 + data1(0) + data2(0)
            cmd(7) = data1(1) + data2(1)
            cmd(8) = data1(2) + data2(2)
            If t1 > 0 And t2 > 0 Then
                buffer1 = data2
                temp1 = t2 - t1
                cdconfig(t1)
            Else
                bgsmell(0) = cmd(6)
                bgsmell(1) = cmd(7)
                bgsmell(2) = cmd(8)
            End If
            send()
        End If
    End Sub
    Public Shared Sub gen(ByVal data1() As Byte, ByVal data2() As Byte, ByVal data3() As Byte, Optional ByVal t1 As Integer = 0, Optional ByVal t2 As Integer = 0, Optional ByVal t3 As Integer = 0)
        If portused = Nothing Then
            If check() = True Then
                gen(data1, data2, data3, t1, t2, t3)
            End If
        Else
            cmd(6) = &H10 + data1(0) + data2(0) + data3(0)
            cmd(7) = data1(1) + data2(1) + data3(1)
            cmd(8) = data1(2) + data2(2) + data3(2)
            If t1 > 0 And t2 > 0 And t3 > 0 Then
                buffer1 = data2
                buffer2 = data3
                temp1 = t2 - t1
                temp2 = t3 - t1
                cdconfig(t1)
            Else
                bgsmell(0) = cmd(6)
                bgsmell(1) = cmd(7)
                bgsmell(2) = cmd(8)
            End If
            send()
        End If
    End Sub
    Public Shared Sub gen(ByVal data1() As Byte, ByVal data2() As Byte, ByVal data3() As Byte, ByVal data4() As Byte, Optional ByVal t1 As Integer = 0, Optional ByVal t2 As Integer = 0, Optional ByVal t3 As Integer = 0, Optional ByVal t4 As Integer = 0)
        If portused = Nothing Then
            If check() = True Then
                gen(data1, data2, data3, data4, t1, t2, t3, t4)
            End If
        Else
            cmd(6) = &H10 + data1(0) + data2(0) + data3(0) + data4(0)
            cmd(7) = data1(1) + data2(1) + data3(1) + data4(1)
            cmd(8) = data1(2) + data2(2) + data3(2) + data4(2)
            If t1 > 0 And t2 > 0 And t3 > 0 And t4 > 0 Then
                buffer1 = data2
                buffer2 = data3
                buffer3 = data4
                temp1 = t2 - t1
                temp2 = t3 - t1
                temp3 = t4 - t1
                cdconfig(t1)
            Else
                bgsmell(0) = cmd(6)
                bgsmell(1) = cmd(7)
                bgsmell(2) = cmd(8)
            End If
            send()
        End If
    End Sub
    Private Shared Function check() As Boolean
        Dim test() As Byte = {&H1, &H0, &H0, &HB, &H6, &H0, &H0, &H0, &H0, &H0, &H0}
        Dim state As Byte
        For Each port As String In My.Computer.Ports.SerialPortNames
            Using com1 As IO.Ports.SerialPort = My.Computer.Ports.OpenSerialPort(port, 19200, IO.Ports.Parity.None, 8, IO.Ports.StopBits.One)
                com1.DtrEnable = True
                com1.Write(test, 0, test.Length)
                com1.ReadTimeout = 500
                Try
                    state = com1.ReadByte()
                Catch exp As System.TimeoutException
                End Try
            End Using
            If state = 0 Then
                portused = port
                Return True
            End If
        Next
        Return False
    End Function
End Class